#include "InComStringHandling.h"

void Ext_InComStrHandle_Str2Hex(const char *sSrc,char *sDest,int nSrcLen)
{
    int  i;

    for(i = 0;i < (nSrcLen / 2);i++)
    {
        char szTmp = 0;
        char szTmp2 = 0;
        char src = sSrc[2 * i];
        char src2 = sSrc[2 * i + 1];

        szTmp = toupper(src) - 0x30;
        if (szTmp > 9)
        {
            szTmp -= 7;
        }

        szTmp2 = toupper(src2) - 0x30;
        if (szTmp2 > 9)
        {
            szTmp2 -= 7;
        }

        sDest[i] = szTmp * 16 + szTmp2;
    }
}

void Ext_InComStrHandle_Hex2Str(const char *sSrc,_OUT char *sDest,int nSrcLen)
{
    int  i;
    char szTmp[3];

    memset(szTmp,0,sizeof(szTmp));

    for(i = 0;i < nSrcLen;i++)
    {
        sprintf(szTmp,"%02x",(unsigned char)sSrc[i]);
        memcpy(&sDest[i * 2],szTmp,2);
    }
}

void Ext_InComStrHandle_Dec2Str(const char *sSrc,char *sDest,int nSrcLen)
{
    int  i;
    char szTmp[4];

    memset(szTmp,0,sizeof(szTmp));

    for(i = 0;i < nSrcLen;i++)
    {
        sprintf(szTmp, "%02d",(unsigned char) sSrc[i]);
        memcpy(&sDest[i * 2],szTmp,2);
    }
}

int Ext_InComStrHandle_Str2Dec(const char *sSrc,_OUT long int *sDest)
{
    char *endptr;

    // 将字符串转换为长整型
    *sDest = strtol(sSrc, &endptr, 10);

    if (endptr == sSrc) {
        fprintf(stderr, "No digits were found!\n");
        return EXIT_FAILURE;
    }

    // 如果endptr不是指向字符串末尾的null字符，则可能有非数字字符
    if (*endptr != '\0') {
        fprintf(stderr, "Non-digit characters were found!\n");
        return EXIT_FAILURE;
    }

    // 输出转换后的数字
    printf("The decimal number is: %ld\n", *sDest);

    return EXIT_SUCCESS;
}

int Ext_InComStrHandle_Byte2Int(char byte)
{
    int val = 0;

    val = toupper(byte) - 0x30;
    if (val > 9)
    {
        val -= 7;
    }

    return val;
}

void Ext_InComStrHandle_StrToHexStr(char* str, char* hex,int str_len)
{
    int i = 0;
    int j = 0;

    for(i = 0;i < str_len;i++)
    {
        hex[j] = (str[i] & 0xf0) >> 4;
        hex[j] = (hex[j] >= 10) ? (hex[j] - 10 + 'a') : (hex[j] + '0');
        j++;
        hex[j] = str[i] & 0x0f;
        hex[j] = (hex[j] >= 10) ? (hex[j] - 10 + 'a') : (hex[j] + '0');
        j++;
    }
}

void Ext_InComStrHandle_Md5BinToHexStr(unsigned char* str, char* hex)
{
    int i = 0;
    int j = 0;

    for(i = 0;i < 16;i++)
    {
        hex[j] = (str[i] & 0xf0) >> 4;
        hex[j] = (hex[j] >= 10) ? (hex[j] - 10 + 'a') : (hex[j] + '0');
        j++;
        hex[j] = str[i] & 0x0f;
        hex[j] = (hex[j] >= 10) ? (hex[j] - 10 + 'a') : (hex[j] + '0');
        j++;
    }
}

void Ext_InComStrHandle_Toupper(char *str)
{
    int i = 0;

    while (str[i] != '\0')
    {
        if ((str[i] >= 'a') && (str[i] <= 'z'))
        {
            str[i] = str[i] + 'A' - 'a';
        }
        else if ((str[i] >= 'A') && (str[i] <= 'Z'))
        {
            str[i] = str[i] + 'a' - 'A';
        }
        i++;
    }
}

void Ext_InComStrHandle_RemoveLastLines(char* str)
{
    int len = strlen(str);

    len--;
    while((str[len] == '\n') || (str[len] == '\r'))
    {
        len--;
    }

    len++;
    str[len] = '\0';
}
